//
//  DyScrollView.m
//  轮播图封装
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "DyScrollView.h"

@implementation DyScrollView{
    UIView *_container;
    CGFloat _width;
    NSInteger _count;
    UIPageControl *_page;
    NSTimer *_mytime;
}
- (instancetype)init{
    self = [super init];
    if (self) {
    }
    return self;
}
- (void)createView:(NSMutableArray *)arr width:(CGFloat)width

{
    _width = width;
    _count = arr.count;
    for (UIView *temp in [self subviews]) {
        if ([temp isKindOfClass:[UIScrollView class]]) {
            [temp removeFromSuperview];
        }
       
    }
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(width * (arr.count + 2), 0);
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    
   
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    // 点击一次跳转
    tap.numberOfTapsRequired = 1;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((arr.count + 1) *width, 0, self.frame.size.width, self.frame.size.height)];
    imageView.userInteractionEnabled = YES;
    imageView.tag = 0;
    [imageView addGestureRecognizer:tap];
    [imageView sd_setImageWithURL:[NSURL URLWithString:[arr firstObject]] placeholderImage:[UIImage imageNamed:@"zan"]];
    [self.scrollView addSubview:imageView];
    
    
    UITapGestureRecognizer *uptap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    // 点击一次跳转
    uptap.numberOfTapsRequired = 1;
    UIImageView *upimageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    upimageView.userInteractionEnabled = YES;
    upimageView.tag = arr.count - 1;
    [upimageView addGestureRecognizer:uptap];
    [upimageView sd_setImageWithURL:[NSURL URLWithString:[arr lastObject]]placeholderImage:[UIImage imageNamed:@"zan"]];
    [self.scrollView addSubview:upimageView];
    
    for (NSInteger i = 0; i < arr.count; i++) {
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        // 点击一次跳转
        tap.numberOfTapsRequired = 1;
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((i+1) * width, 0, self.frame.size.width, self.frame.size.height)];
        imageView.userInteractionEnabled = YES;
        imageView.tag = i;
        [imageView addGestureRecognizer:tap];
        [imageView sd_setImageWithURL:[NSURL URLWithString:arr[i]] placeholderImage:[UIImage imageNamed:@"zan"]];
        [self.scrollView addSubview:imageView];
    }
    [self.scrollView setContentOffset:CGPointMake(width, 0)];
    if (!_time) {
        _time = 5;
    }
    
    if (!_mytime) {
       _mytime =  [NSTimer scheduledTimerWithTimeInterval:_time target:self selector:@selector(nextScrollView) userInfo:nil repeats:YES];
    }
    
    if (!_page) {
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.frame.size.width - _count * 15, self.frame.size.height - 40, _count * 15, 50)];
    }
    [self addSubview:_page];
    _page.numberOfPages = arr.count;
    _page.currentPage = 0;
    
    
}


- (void)tapAction:(UITapGestureRecognizer *)tap{
    NSInteger i = tap.view.tag;
    [self.delegate takeValue:i];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    if (self.scrollView.contentOffset.x ==(_count + 1) *_count) {
        self.scrollView.contentOffset = CGPointMake(_width, 0);
        _page.currentPage = 0;
    }
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(_count *_width, 0);
        _page.currentPage = _count;
    }
    NSInteger p = (self.scrollView.contentOffset.x / _width) - 1 ;
    _page.currentPage = p;
    
    
}
- (void)nextScrollView{
    [self.scrollView setContentOffset:CGPointMake(_width +_scrollView.contentOffset.x, 0) animated:YES];
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.scrollView.contentOffset.x ==(_count + 1) *_width) {
        self.scrollView.contentOffset = CGPointMake(_width, 0);
        _page.currentPage = 0;
    }
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(_count *_width, 0);
        _page.currentPage = _count;
    }
    NSInteger p = (self.scrollView.contentOffset.x / _width) - 1 ;
    _page.currentPage = p;
    
}


@end
